package com.tiancheng.trade.settlement.controller.admin;

import com.tiancheng.trade.commom.core.data.PageInfo;
import com.tiancheng.trade.commom.core.data.Result;
import com.tiancheng.trade.settlement.bo.SRevenueDetailQueryResBo;
import com.tiancheng.trade.settlement.bo.SRevenueSummaryBO;
import com.tiancheng.trade.settlement.enums.SystemSourceEnum;
import com.tiancheng.trade.settlement.service.settlement.ISRevenueDetailService;
import com.tiancheng.trade.settlement.util.JsonUtil;
import com.tiancheng.trade.settlement.vo.settlement.admin.BaseRevenueQueryVo;
import com.tiancheng.trade.settlement.vo.settlement.api.NcCloudReqVO;
import com.tiancheng.trade.settlement.vo.settlement.api.NcCloudResVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 收入统计
 *
 * @author lzh
 */
@RestController
@RequestMapping("/admin/revenue")
@Validated
@Slf4j
public class SRevenueDetailAdminController {

    @Resource
    private ISRevenueDetailService revenueDetailService;

    /**
     * 收入汇总信息查询
     *
     * @param revenueQueryVo
     * @return
     */
    @GetMapping(value = "/summary")
    public Result<PageInfo<SRevenueSummaryBO>> summary(@Valid BaseRevenueQueryVo revenueQueryVo) {
        List<SRevenueSummaryBO> list = revenueDetailService.revenueSummary(revenueQueryVo);
        if(!CollectionUtils.isEmpty(list)){
            return Result.success(new PageInfo<SRevenueSummaryBO>(list, new PageInfo.Pagination(revenueQueryVo.getCurrentPage(), revenueQueryVo.getPageSize(), list.size())));
        }
        return Result.success(new PageInfo<SRevenueSummaryBO>());
    }

    /**
     * 收入汇总信息-导出
     *
     * @param revenueQueryVo
     * @return
     */
    @GetMapping(value = "/summary/export")
    public Result exportSummary(@Valid BaseRevenueQueryVo revenueQueryVo, HttpServletRequest request, HttpServletResponse response) throws IOException, IllegalAccessException {
        return revenueDetailService.exportSummary(revenueQueryVo, request, response);
    }

    /**
     * 收入明细信息查询
     *
     * @param revenueQueryVo
     * @return
     */
    @GetMapping(value = "/detail")
    public Result<PageInfo<SRevenueDetailQueryResBo>> detail(@Valid BaseRevenueQueryVo revenueQueryVo) {
        return Result.success(revenueDetailService.revenueDetail(revenueQueryVo));
    }


    /**
     * 收入明细信息查询
     *
     * @param revenueQueryVo
     * @return
     */
    @GetMapping(value = "/detail/export")
    public Result exportDetail(@Valid BaseRevenueQueryVo revenueQueryVo, HttpServletRequest request, HttpServletResponse response) throws IOException, IllegalAccessException {
        return revenueDetailService.exportDetail(revenueQueryVo, request, response);
    }

    /**
     * 收入汇总信息查询
     *
     * @return
     */
    @GetMapping(value = "/allSystemSource")
    public Result<List<Map<String,String>>> getAllSystemSource() {
        List<Map<String, String>> maps = new ArrayList<>(SystemSourceEnum.values().length);
        for (SystemSourceEnum value : SystemSourceEnum.values()) {
            Map<String, String> map = new HashMap<>();
            map.put("system_source",value.getCode());
            map.put("system_source_name",value.getDesc());
            maps.add(map);
        }
        return Result.success(maps);
    }

    /**
     * NC取数对接
     * @param ncCloudReqVO
     * @return
     */
    @PostMapping(value = "/statistics")
    public Result<PageInfo<NcCloudResVO>> ncCloudGet(@RequestBody  @Valid NcCloudReqVO ncCloudReqVO){
        log.info("nc取数查询入参 : "+ JsonUtil.toJson(ncCloudReqVO));
        return Result.success(revenueDetailService.ncCloudGet(ncCloudReqVO));
    }

}
